package com.nccsjz.back.news.service;
import java.sql.SQLException;
import java.util.List;

import com.nccsjz.back.cardbuy.dao.CardBuyDAO;
import com.nccsjz.back.news.dao.NewsDAO;
import com.nccsjz.base.BaseService;
import com.nccsjz.pojo.News;
import com.nccsjz.utils.Page;
import com.nccsjz.utils.Pager;
/**
 * 用于处理新闻相关的业务逻辑
 * ＠author stone
 * @version 1.0
 * */
public class NewsService  extends BaseService{
	//根据主键查看一条新闻
	public News getNews(long id) throws SQLException{
		News news=null;
		try{
			openCon();
			news=new NewsDAO(con).getNews(id);
		}finally{
			closeCon();
		}
		return news;
	}
	//添加一条新闻
	public boolean addNews(News news) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			flag=new NewsDAO(con).addNews(news);
		}finally{
			closeCon();
		}
		return flag;
	}
	//删除一条新闻
	public boolean deleteNews(long id) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			flag=new NewsDAO(con).deleteNews(id);
			
		}finally{
			closeCon();
		}
		return flag;
	}
	//分有处理新闻
	public Pager getNews(int pageSize,int pageNo) throws SQLException{
		Pager pager=null;
		try{
			openCon();
			pager=new NewsDAO(con).getNews(pageSize, pageNo);
		}finally{
			closeCon();
		}
		return pager;
	}
	//修改新闻
	public boolean updateNews(News news) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			flag=new NewsDAO(con).updateNews(news);
		}finally{
			closeCon();
		}
		return flag;
	}
	//批量删除新闻
	public boolean deleteBatchNews(List<Long> list) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			con.setAutoCommit(false);
			flag=new NewsDAO(con).deleteBathNews(list);
			con.commit();
		}finally{
			closeCon();
		}
		return flag;
	}
	
	//add by changpf
	 /**
	  * 得到前n条新闻。
	  * @return
	  */
	public List<News> getTopN(int n) {
		List<News> news=null;
		try {
			openCon();
			news=new NewsDAO(con).getTopN(n);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				closeCon();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	return news;
	}
	
	/**
	 * findNews获取新闻分页信息
	 * 
	 * @param page
	 * @throws SQLException
	 */
	public void findNews(Page page) throws SQLException {
		try {
			openCon();			 
			NewsDAO newsDAO = new NewsDAO(con);
			//查询数据
			newsDAO.getNewsPage(page);
			page.setRecordCount(newsDAO.getRecordCount());
		} finally {
			closeCon();
		}
	}
}
